package edu.szte.ovrt.datahandler.core;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

import static org.junit.Assert.*;

public class IOMetaFactoryTest {
	@Test
	public void testGetInstance() {
		final IOMetaFactory instance = IOMetaFactory.getInstance();
		assertNotNull(instance);
	}

	@Test
	public void testGetInstanceSame() {
		final IOMetaFactory instance1 = IOMetaFactory.getInstance();
		final IOMetaFactory instance2 = IOMetaFactory.getInstance();
		assertSame(instance1, instance2);
	}

	@Test
	public void testGetExtensionNoExtension() throws Exception {
		assertEquals("", getExtensionAccessor("fileNameWithOutExtension"));
	}

	@Test
	public void testGetExtensionEmptyExtension() throws Exception {
		assertEquals("", getExtensionAccessor("fileNameWithOutExtension."));
	}

	@Test
	public void testGetExtensionEmptyFileName() throws Exception {
		assertEquals("extension", getExtensionAccessor(".extension"));
	}

	@Test
	public void testGetExtensionOneExtension() throws Exception {
		assertEquals("extension", getExtensionAccessor("fileNameWith.extension"));
	}

	@Test
	public void testGetExtensionMoreExtensions() throws Exception {
		assertEquals("extension", getExtensionAccessor("file.name.with.extension"));
	}

	@Test
	public void testGetExtensionWithPathNoExtension() throws Exception {
		assertEquals("", getExtensionAccessor("path/without/dot/inside/fileNameWithOutExtension"));
	}

	@Test
	public void testGetExtensionWithPathEmptyExtension() throws Exception {
		assertEquals("", getExtensionAccessor("path/without/dot/inside/fileNameWithOutExtension."));
	}

	@Test
	public void testGetExtensionWithPathEmptyFileName() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/without/dot/inside/.extension"));
	}

	@Test
	public void testGetExtensionWithPathOneExtension() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/without/dot/fileNameWith.extension"));
	}

	@Test
	public void testGetExtensionWithPathMoreExtension() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/without/dot/file.name.with.extension"));
	}

	@Test
	public void testGetExtensionDotInPathNoExtension() throws Exception {
		assertEquals("", getExtensionAccessor("path/with.dot/inside/fileNameWithOutExtension"));
	}

	@Test
	public void testGetExtensionDotInPathEmptyExtension() throws Exception {
		assertEquals("", getExtensionAccessor("path/with.dot/inside/fileNameWithOutExtension."));
	}

	@Test
	public void testGetExtensionDotInPathEmptyFileName() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/with.dot/inside/.extension"));
	}

	@Test
	public void testGetExtensionDotInPathOneExtension() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/with.dot/inside/fileNameWith.extension"));
	}

	@Test
	public void testGetExtensionDotInPathMoreExtension() throws Exception {
		assertEquals("extension", getExtensionAccessor("path/with.dot/inside/file.name.with.extension"));
	}

	private static String getExtensionAccessor(final String fileName) throws Exception {
		return Whitebox.invokeMethod(IOMetaFactory.class, "getExtension", fileName);
	}
}
